package ysq.xyg.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.dao.MyQuestionDao;
import ysq.xyg.model.MyQuestion;
import ysq.xyg.service.MyQuestionService;
import ysq.xyg.util.BaseUtil;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author yu sheng qiang
 * @create 2017-03-22 15:36
 **/
@Service("myQuestionService")
public class MyQuestionServiceImpl extends BaseServiceImpl<MyQuestion> implements MyQuestionService {


    @Resource
    private MyQuestionDao myQuestionDao;
    @Autowired
    public void setMyQuestionDao(MyQuestionDao myQuestionDao) {
        this.baseDao = myQuestionDao;
    }
    @Override
    public PageInfo<MyQuestion> pageInfoLeft(MyQuestion myQuestion, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        if(StringUtil.isNotEmpty(myQuestion.getNickName())){
            myQuestion.setNickName(BaseUtil.encodeBase64String(myQuestion.getNickName()));
        }
        List<MyQuestion> list = myQuestionDao.leftList(myQuestion);
        return new PageInfo<MyQuestion>(list);
    }
    @Override
    public PageInfo<MyQuestion> pageInfo(MyQuestion myQuestion, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        // 通用Example查询
        Example example = new Example(MyQuestion.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(myQuestion.getMessage())) {
            criteria.andLike("name", "%" + myQuestion.getMessage() + "%");
        }
        if (StringUtils.isNotEmpty(myQuestion.getQuestionId())) {
            criteria.andLike("questionId", myQuestion.getQuestionId());
        }
        List<MyQuestion> list = myQuestionDao.selectByExample(example);
        return new PageInfo<MyQuestion>(list);
    }
    @Override
    public List<MyQuestion> list(MyQuestion myQuestion) {
        // 通用Example查询
        Example example = new Example(MyQuestion.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(myQuestion.getMessage())) {
            criteria.andLike("name", "%" + myQuestion.getMessage() + "%");
        }
        if (StringUtils.isNotEmpty(myQuestion.getQuestionId())) {
            criteria.andLike("questionId", myQuestion.getQuestionId());
        }
        List<MyQuestion> list = myQuestionDao.selectByExample(example);
        return list;
    }
}
